/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.vuln.core.scap.nvd;

import org.exolab.castor.mapping.GeneralizedFieldHandler;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * A Castor FieldHandler for the NVD date/time.
 *
 * @author  Akihito Nakamura, AIST
 * @version $Id: NvdDatetimeHandler.java 9 2010-05-15 10:06:23Z nakamura5akihito $
 */
public class NvdDatetimeHandler
    extends GeneralizedFieldHandler
{

    /**
     * The date/time formatter and parser.
     */
    private SimpleDateFormat  _formatter;


    /**
     * The default date format pattern.
     */
    public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss";




    /**
     * Constructor.
     */
    public NvdDatetimeHandler()
    {
        this( PATTERN );
    }



    /**
     * Constructor.
     *
     * @param   pattern
     *  the date/time format pattern description.
     */
    public NvdDatetimeHandler( final String pattern )
    {
        _formatter = new SimpleDateFormat( pattern );
    }



    /**
     */
    public void setPattern( final String pattern )
    {
        _formatter = new SimpleDateFormat( pattern );
    }



    ////////////////////////////////////////////////////////////////
    //  extends GeneralizedFieldHandler
    ////////////////////////////////////////////////////////////////

    // from Date to String
    public Object convertUponGet( Object value )
    {
        if (value == null) {
            return null;
        }

        String  s = _formatter.format( (Date)value );

        return s;
    }



    // from String to Date
    //
    public Object convertUponSet( Object value )
    {
        Date  date = null;
        if (value != null) {
            try {
                date = _formatter.parse( (String)value );
            } catch (ParseException p_ex) {
                throw new IllegalArgumentException( p_ex.getMessage() );
            }
        }

        return date;
    }



    public Class<Date> getFieldType()
    {
        return Date.class;
    }



    public Object newInstance( Object parent )
        throws IllegalStateException
    {
        return null;
    }

}
// NvdDatetimeHandler

/* vim:set tabstop=4:set expandtab:set shiftwidth=4: */

